<template>
	<view>
		<view class="u-search-box">
			<u-search placeholder="课程搜索" v-model="keyword" @input="" @search="searchcourse"></u-search>
		</view>
		<!-- <view class="test" > -->
		<!-- <u-swiper v-for="item in courselist" :key="item" :list="list" @click="getCouse(item)"></u-swiper> -->
		<u-swiper :list="list" @click="click()"></u-swiper>
		<!-- </view> -->
		<view>
			<uni-card title="精选好课" isFull="false">
				<!--可删除 做成for循环-->
				<view class="test" v-for="(item,index) in courselist" v-if="index<10" :key="item">
					<uni-card margin="10rpx" isFull="true" @click="getCouse(item)">
						<image style=" width: 300rpx; height: 210rpx;" :src="item.img" mode="scaleToFill"></image>
						<view style="height: 30px; display: flex; align-items: center; text-align: center;">{{item.courseName}}</view>
						<view style="margin-top: 5px;">
							<uni-rate :disabled="true" :size="10" :max="5" :value="item.star"></uni-rate>
						</view>
					</uni-card>
				</view>
			</uni-card>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				skey:"",
				keyword: "",
				courselist: {},
				searchlist:{},
				randlist:{},
				index: ""
			}
		},
		onLoad() {
			this.tenrand()
		},
		onShow() {
			this.getcourselist();
			this.getsearchlist()
			this.suanfa()
			this.tenrand()
		},
		methods: {
			click(index) {
				//click回调事件，index对当前点击个数 0开始
				this.index = index;
				this.getCouse(this.list[index]);
			},
			getcourselist() {
				this.list = [];
				this.$http("/aplication", {
						status: "1"
					})
					.then((res) => {
						
						for (let i = 0; i < 4; i++) {
							var data = new Object();
							data.image = res[i].img;
							//添加data的id数据
							data.courseid = res[i].courseid;
							this.list.push(data)
						}
					})
			},
			getCouse(even) {
				console.log(even);
				uni.navigateTo({
					url: '../mycourses/addCourse?id=' + even.courseid,
				});
			},
			searchcourse(value) {
				uni.setStorage({
					key:'skeyword',
					data:this.keyword,
					success:function(){
						console.log(this.keyword);
					}
				})
				/*console.log(value);
				this.$http("/search",{
					value
				})
				.then((res) =>{
					console.log(res);
					var coursejs = JSON.stringify(res);
					//console.log(coursejs);*/
				uni.navigateTo({
					url: "../mycourses/searchcourse?value=" + value
				})
				//})
				//console.log("搜索");
			},
			suanfa() {
				//this.list = [];
				var this_ = this;
				this.$http('/getRecommend', {
					id: uni.getStorageSync("stuid")
				}).then((res) => {
					console.log(res);
					this_.courselist = res;
					// for(let j=0; j < this_.searchlist.length; j++){
					// 	this_.courselist.push(this_.searchlist.pop);
						
					// }
					this_.courselist = 	this_.courselist.concat(this_.searchlist);
					if(this_.courselist.length<10)
					{
						this_.courselist = 	this_.courselist.concat(this_.randlist);
					}
					console.log(this_.courselist);
					for (let i = 0; i < this_.courselist.length; i++) {
						var data = new Object();
						data.image = this_.courselist[i].img;
						//this.list.push(data)
					}
				})
			},
			getsearchlist() {
				var that = this;
				uni.getStorage({
				    key: 'skeyword',
				    success: function (res) {
						console.log(111);
				        console.log(res.data);
						that.skey=res.data;
						console.log(that.skey);
				    },
					complete:function(){
				    	console.log(that.skey)
				    }
				});
				this.$http("/search",{
					value : that.skey.toString()
				})
				.then((res) =>{
					that.searchlist = res;
					console.log(that.searchlist);
					
				})
				
			},
			tenrand(){
				this.$http('/tenrand', {
				}).then((res) => {
					this.randlist=res;
					console.log(res);
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		padding: 40rpx;
	}

	.test {
		height: 150px;
		width: 145px;
		margin: 10px, 10px, 10px, 10px;
		float: left;
	}
	
	.u-search-box {
		padding: 18rpx 30rpx;
	}
</style>
